﻿using System;
using Foothill.Common;
using Foothill.IoC;
using Foothill.Service;
using Foothill.Service.Email;
using Foothill.Service.Session;
using Foothill.Service.AdService;
using Foothill.Model;
using Foothill.Web.PostForSale.App_LocalResources;

namespace Foothill.Web
{
    public partial class PostForSaleConfirmation : System.Web.UI.Page
    {
        private int _maskedAdId { set; get; }
        private string _editKey { set; get; }
        private string _contactEmail { set; get; }

        private ISessionService _sessionService;
        private IEmailService _emailService;
        private IPostValidator _validator;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            _sessionService = IocContainer.GetInstance<ISessionService>();
            _emailService = IocContainer.GetInstance<IEmailService>();
            _validator = IocContainer.GetInstance<IPostValidator>();

            object obj = _sessionService.GetSessionValue(SessionNames.AdPostSucceedInfo);
            AdPostSucceedInfo info = obj as AdPostSucceedInfo;
            if (info != null)
            {
                _maskedAdId = SecurityUtility.MaskEditPostId(info.AdId);
                _editKey = info.EditKey;
                _contactEmail = info.Email;

                lblAdId.Text = _maskedAdId.ToString();
                lblEditKey.Text = _editKey;
                txtEmail.Text = _contactEmail;
            }
        }

        protected void SendEmailClick(object sender, EventArgs e)
        {
            _contactEmail = txtEmail.Text.Trim();

            if (_validator.ValidateEmailAddress(_contactEmail))
            {
                if (_emailService.SendNewPostNotification(_maskedAdId, _editKey, _contactEmail))
                {
                    lblFeedback.Text = PostForSaleConfirmation_aspx.SendEmailOk;
                    lblFeedback.CssClass = "send-succeed";
                }
                else
                {
                    lblFeedback.Text = PostForSaleConfirmation_aspx.SendEmailFailed;
                    lblFeedback.CssClass = "send-failed";
                }              
            }
            else
            {
                lblFeedback.Text = PostForSaleConfirmation_aspx.InvalidEmail;
                lblFeedback.CssClass = "send-failed";
            }
        }
    }
}
